package com.simple.mrchip.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.simple.mrchip.core.HttpAccess;
import com.simple.mrchip.core.HttpAccessListener;

public class RegisterActivity extends AppCompatActivity implements HttpAccessListener {

    private Button mRegisterButton;
    private EditText mUserName;
    private EditText mPassword;
    private EditText mPassword2;
    private HttpAccess httpAccess;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        setContentView(R.layout.activity_register);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        httpAccess = HttpAccess.getInstance();
        httpAccess.setResponser("Register", this);
        mRegisterButton = (Button) findViewById(R.id.btRegister2);
        mUserName = (EditText) findViewById(R.id.txtRgUserName);
        mPassword = (EditText) findViewById(R.id.txtRgPassword);
        mPassword2 = (EditText) findViewById(R.id.txtRgPassword2);
        mRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = mUserName.getText().toString();
                String password = mPassword.getText().toString();
                String password2 = mPassword2.getText().toString();
                if(userName.isEmpty() || password.isEmpty() || password2.isEmpty()) {
                    return;
                }
                if (!password.equals(password2)) {
                    Toast.makeText(getBaseContext(), "Password not match", Toast.LENGTH_SHORT).show();
                    mPassword.setText("");
                    mPassword2.setText("");
                    return;
                }
                httpAccess.register(userName, password);
            }
        });
    }

    @Override
    public void getHttpData(String response) {
        Log.d("Login", response);
        if (response.equals("Error") || response.equals("Existed")) {
            return;
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("UUID", response);
        editor.putString("UserName", mUserName.getText().toString());
        editor.apply();
        Intent intent = new Intent(RegisterActivity.this, AppActivity.class);
        startActivity(intent);
        this.finish();
    }
}
